package com.daoyin.ssph.action;

import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.springframework.web.portlet.bind.annotation.ResourceMapping;


import com.daoyin.ssph.bean.Active;
import com.daoyin.ssph.bean.Administrator;
import com.daoyin.ssph.service.ActivitysService;

public class ActivitysAction {
	@Resource
	private String id;
	private String title;
	private String content;
	private String time;
	public void setId(String id) {
		this.id = id;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public void setContent(String content) {
		this.content = content;
	}
	public void setTime(String time) {
		this.time = time;
	}
	private ActivitysService act;

	public void setAct(ActivitysService act) {
		this.act = act;
	}
	
	public String add() throws UnsupportedEncodingException{
		ServletActionContext.getRequest().setCharacterEncoding("utf-8");
		Active t = new Active();
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//�������ڸ�ʽ
		time = df.format(new Date());
		t.setTitle(title);
		t.setContent(content);
		t.setTime(time);
		t.setPublish(0);
		String str = act.add(t);
		if(("success").equals(str)){
			return "success";
		}
		return "fail";			
	}
	
	public String update() throws UnsupportedEncodingException{
		ServletActionContext.getRequest().setCharacterEncoding("utf-8");
		Active t = act.query(Integer.parseInt(id));
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//�������ڸ�ʽ
		time = df.format(new Date());
		t.setId(t.getId());
		t.setTitle(title);
		t.setContent(content);
		t.setTime(time);
		t.setPublish(0);
		String str = act.update(t);
		if(("success").equals(str)){
			return "success";
		}
		return "fail";			
	}
	
	public String publish(){
		Active t = act.query(Integer.parseInt(id));
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//�������ڸ�ʽ
		time = df.format(new Date());
		t.setId(t.getId());
		t.setTitle(t.getTitle());
		t.setContent(t.getContent());
		t.setTime(time);
		t.setPublish(1);
		String str = act.update(t);
		if(("success").equals(str)){
			return "success";
		}
		return "fail";	
		
		
		
	}
	
	
	public String show(){
		HttpServletRequest request = ServletActionContext.getRequest();
		List<Active> list = act.query();
		request.setAttribute("show", list);
		return "success";
	
	}
	public String showDetails(){
		HttpServletRequest request = ServletActionContext.getRequest();
		Active active = act.query(Integer.parseInt(id));
		request.setAttribute("activitys", active);
		return "success";
	}
	public String delete(){
		act.delete(Integer.parseInt(id));
		return "success";
	}
}
